package com.ruoyi.web.controller.app;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.PaOrderStatus;
import com.ruoyi.system.domain.PaOrder;
import com.ruoyi.system.domain.PxBox;
import com.ruoyi.system.domain.bo.PxBoxBo;
import com.ruoyi.system.domain.bo.SupplierOrderBo;
import com.ruoyi.system.domain.bo.SupplierUpdateOrderBo;
import com.ruoyi.system.domain.vo.PxBoxVo;
import com.ruoyi.system.domain.vo.SupplierOrderVo;
import com.ruoyi.system.domain.vo.SystemOrderReponseVo;
import com.ruoyi.system.mapper.PaOrderMapper;
import com.ruoyi.system.mapper.PxBoxMapper;
import com.ruoyi.system.service.IPaOrderService;
import com.ruoyi.system.service.IPxBoxService;
import com.ruoyi.system.service.ISupplierOrderService;
import com.ruoyi.system.service.IUserPxBoxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName SupplierOrderController
 * @Description 供应商订单信息表
 * @date 2024/7/4 19:03
 * @Version 1.0
 * @Auther: zhangzhenyu
 */
@RestController
@RequestMapping("/supplier/order")
@Api(tags = "供应商订单信息表")
public class SupplierOrderController extends BaseController {

    @Autowired
    private ISupplierOrderService suppliers;


    @Autowired
    private PaOrderMapper paOrderMapper;
    @Autowired
    private PxBoxMapper pxBoxMapper;

    @Autowired
    private IUserPxBoxService userPxBoxService;


    @Log(title = "供应商查询代购订单列表（purchase agency）", businessType = BusinessType.INSERT)
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public TableDataInfo<SupplierOrderVo> list(@Validated @RequestBody SupplierOrderBo supplierOrderBo)
    {
        supplierOrderBo.setSupplierId(getUserId());
        return suppliers.selectPaOrderList(supplierOrderBo);
    }

    /**
     * 报价（purchase agency）
     */
    @Log(title = "报价（purchase agency）", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "报价（purchase agency）", notes = "报价（purchase agency）")
    public R edit(@RequestBody SupplierUpdateOrderBo supplierOrderBo)
    {
        //获取订单信息
        PaOrder paOrder = paOrderMapper.selectPaOrderByOrderId(supplierOrderBo.getOrderId());
        if (paOrder == null) {
            return R.fail("订单不存在");
        }
        if (!paOrder.getStatus().equals(PaOrderStatus.QUOTING.getCode())) {
            return R.fail("当前订单不允许修改");
        }
        //判断是否有拼箱单
        PxBox pxBox = pxBoxMapper.selectPxBoxByOrderId(supplierOrderBo.getOrderId());
        if (Objects.isNull(pxBox)) {
            return R.fail(HttpStatus.ORADER_STATUS_ERROR,"请填写拼箱信息");
        }
        supplierOrderBo.setSupplierId(getUserId());
        supplierOrderBo.setSupplierName(getUsername());

        return R.ok(suppliers.updatePaOrder(supplierOrderBo));
    }
}
